package com.dayuanit.dy17.atm.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.dayuanit.dy17.atm.entity.Card;
import com.dayuanit.dy17.atm.entity.Recharge;
import com.dayuanit.dy17.atm.exception.BizException;
import com.dayuanit.dy17.atm.mapper.CardMapper;
import com.dayuanit.dy17.atm.mapper.RechargeMapper;
import com.dayuanit.dy17.atm.service.IRechargeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class RechargeServiceImpl implements IRechargeService {

    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private CardMapper cardMapper;

    @Override
    public String rechargeCard(long cardId, String amount, String pwd, long userId) {
        //支付宝充值
        //参数校验
        Card card = cardMapper.selectByPrimaryKey(cardId);
        if (null == card) {
            throw new BizException("银行卡不存在或者密码错误");
        }

        if (!card.getPassword().equals(pwd)) {
            throw new BizException("银行卡不存在或者密码错误");
        }

        if (card.getUserId() != userId) {
            throw new BizException("你无权操作");
        }

        if (StringUtils.isBlank(amount)) {
            throw new BizException("请输入充值金额");
        }

        if (Double.parseDouble(amount) <= 0) {
            throw new BizException("充值金额必须大于0");
        }

        //生成支付订单
        Recharge recharge = new Recharge();
        recharge.setAmount(new BigDecimal(amount));
        recharge.setCardId(cardId);
        recharge.setCreateTime(new Date());
        recharge.setModifyTime(recharge.getCreateTime());
        recharge.setStatus(1);
        recharge.setUserId(userId);
        rechargeMapper.insert(recharge);

        //生成支付宝收银台地址
        AlipayClient alipayClient = new DefaultAlipayClient("http://payhub.dayuanit.com/gateway/alipay/web.do",
                "2021020116465961741",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCep1lsWxKLAOfQrHCe6DTnvydp JNjbJH8s0XlQK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IujCPNwbUA9aXwqEytWUTbBT6pT1L9tqwaOHhm0N267aRYUS4dhXqGKx5pFvt8fz bMDKsgqA3F+MlYY8Xv5ECc5c1GCf74xG+RWwkLWG5aH13+MvzD220VkDAoGAYGInk4x3Rj84k4zA roYqdc8x8GEPSUc1f3T5QaGVdRwD20b6iCG7BIAzUW8UporPmtA5+Kze3Wd6Q05PqNaw1CqKKVJv f3UfZOaBy+h3VvbTmEmWzlA1d48uqRBgb1lQT4Z8JSjRofX8WO/UwJQ6C7vGafU3MxMcfgLHs1eR 7L0CgYEA9RcOYdb5xDl1dYNJ4nq3Fn0HycVDOwG5jOsdfdcf3b6J/nJNWnUJ+4PcoC/GSayPRfAo mFbPJ/Dv32OreIDSBuRzO0xjGubrFRUCIvEbP8gk7wJw3T/QDWXHVyvVO/nzhTouxhcfONmZ/544 VhIFK4Hl3A80MZT2UQhX1DyXi9s=",
                "json",
                "UTF-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4SZgs0ZcIyvfS4hv2WEY PtoLpTCmza2IQKsLWxXHWwIFgPsnDB61jvNG9Oij7pmxx/+JMOWkcdVj1ltoRShH Dy3HAelIYxXnpKnHI3x9c6PyYUIX+szZ/dlkCEEbJPQ0tQqvkauK2ue3gDTBeWw/ frSbRUh0sQu47IUX8XO0MoUZDo62/5mnRnq4S+BKltL08eV7Cb5U+d/sIoce/IuR rDe9IKzdRk+efsSGK52nttuMa5NBOXpZrOg4MdLlhepc/g5xuZAIHkn+MAlW/gRD NfKvfOHh49ghpkO8ilNCQEC3KTn6uccl1DfxWcbgm0Slk5d/4XJ0dJWkarcsTcr2 nQIDAQAB",
                "RSA2");

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl("http://127.0.0.1:9999");
        request.setNotifyUrl("http://atm.stepboot.com:10000/pay/alipayNotify");

        request.setBizContent("{" +
                "\"out_trade_no\":\""+recharge.getId()+"\"," +
                "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "\"total_amount\":\""+recharge.getAmount().toString()+"\"," +
                "\"subject\":\"ATM充值\"," +
                "\"body\":\"ATM银行卡充值\"" +
                "  }");
        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

        String formData = response.getBody();
        System.out.println("alipay URL result=" + formData);

        return formData;
    }
}
